<?php
namespace app\controllers;

use app\models\KuaidiRecorder;

class KuaidiController extends BaseController{
    public function actionAdd(){
        $post = \Yii::$app->request->post();
        $id = $post['KuaidiRecorder']['id'];
        if($id){
            $model = KuaidiRecorder::find()->where('id=:id',[':id' => $id])->one();
            if(!$model){
                $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
                return json_encode($result);
            }
        }else{
            $model = new KuaidiRecorder();
            $model->addtime = time();
        }
        $result = $model->add($post);
        return json_encode($result);
    }

    public function actionList(){
        $list = KuaidiRecorder::find();
        $list = $list->orderBy('addtime DESC')->asArray()->all();
        foreach($list as $k=>$v){
            $list[$k]['addtime'] = date('Y-m-d H:m:s',$v['addtime']);
        }
        $result = ['result'=>1,'data'=>$list];
        return json_encode($result,JSON_NUMERIC_CHECK);
    }

    public function actionOne(){
        $id = \Yii::$app->request->post('id');
        $model = KuaidiRecorder::find()->where('id=:id',[':id' => $id])->asArray()->one();
        if(!$model){
            $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }
        $result = ['result'=>1,'data'=>$model];
        return json_encode($result);
    }

    public function actionDel(){
        $id = \Yii::$app->request->post('id');
        $model = KuaidiRecorder::find()->where('id=:id',[':id' => $id])->one();
        if(!$model){
            $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return  json_encode($result);
        }
        $model->delete();
        $result = ['result'=>1,'data'=>'操作成功'];
        return json_encode($result);
    }

}
